package edu.nf.util.sms;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;

/**
 * @Date 2023-06-19
 * @Author qiu
 * <p>
 * 发送短信的工具类
 */
public class SMSUtil {

    // 调用生成验证码的工具类
    FourKeyUtil fourKeyUtil = new FourKeyUtil();

    // 定义一个空的字符串，存储验证码
    String key = null;

    public String send(String phone) {
        try {
            // 创建 COSCredentials 对象，传入 SecretId 和 SecretKey --- 密钥
            Credential cred = new Credential("AKIDxmSxGE3g5GEzmPs8A2uc6LM9j4D8o57x", "f5II5g4JMCpEPL5bw4zhCUaYIqBOPFtJ");
            HttpProfile httpProfile = new HttpProfile();
            // 设置腾讯云服务 API 的访问域名
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            // 设置 HTTP 的请求配置信息
            clientProfile.setHttpProfile(httpProfile);
            // 指定 SMS 所在的区域
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);

            SendSmsRequest req = new SendSmsRequest();
            // 接收短信的手机号码，可以设置多个
            String[] phoneNumberSet1 = {"+86" + phone};
            req.setPhoneNumberSet(phoneNumberSet1);

            // 设置正文模板 ID
            req.setTemplateID("1834241");
            // 设置短信应用 ID
            req.setSmsSdkAppid("1400830872");
            // 设置签名内容
            req.setSign("天狗租车公众号");

            // 获取验证码
            key = fourKeyUtil.keyUtils();

            // 短信模板中的参数，需与短信模板中的参数个数一致
            String[] templateParams = {key};
            req.setTemplateParamSet(templateParams);

            // 发送短信
            //SendSmsResponse resp = client.SendSms(req);
            //System.out.println(SendSmsResponse.toJsonString(resp));

        } catch (Exception e) { //TencentCloudSDKException
            System.out.println(e.toString());
        }

        // 返回验证码
        return "123456";
    }

}
